package com.china08.yxyapi.api.manage.sch.office.innernotice;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 学校管理员 内部通知 按分组发送通知
 * 
 */
@CrossOrigin
@RestController("SchInnernoticeCrtByGroupIdLstController")
public class InnernoticeCrtByGroupIdLstController extends YxyApiBaseController {

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/office/innernotice/crtByGroupIdLst", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        RespModel respModel = new RespModel();
        // 当前用户id
        String currentUserId = getProfile(request, response).getId();
        // 当前用户学校
        String schoolId = getProfile(request, response).getSchoolId();
        // 接受人id
        Set<String> allUserIdSet = new HashSet<String>();
        String receiverGroupIds = StringUtils.join(reqModel.receiver, ",");
        // 根据组id 查询所有的teaid
        Map<String, List<String>> map1 = restTemplate
                .getForObject(yxyApiYxUrl + "/comm/noticeGroup/findGroupTeaId/" + receiverGroupIds, Map.class);

        // 去重
        allUserIdSet.addAll(map1.get("teacherIdFromGroupIdLst"));
        // 将所有添加的老师id 放到一个集合中
        List<String> userIdLst = new ArrayList<>(allUserIdSet);

        List<YxyNotifictionTea> receiver = new ArrayList<YxyNotifictionTea>();
        YxyNotifictionTea yxyNotifictionTea = null;
        Map<String, String> map2 = null;
        for (String userId : userIdLst) {
            yxyNotifictionTea = new YxyNotifictionTea();
            yxyNotifictionTea.setUserId(userId);
            map2 = restTemplate.getForObject(yxyApiYxUrl + "/comm/user/" + userId, Map.class);
            String userNick = map2.get("userNick") == null ? "" : map2.get("userNick");
            yxyNotifictionTea.setUserNick(userNick);
            yxyNotifictionTea.setIsDelete(0);
            yxyNotifictionTea.setIsRead(0);
            receiver.add(yxyNotifictionTea);
        }

        // ###
        // 查询学校信息和用户信息
        Map<String, String> map = restTemplate.getForObject(
                yxyApiYxUrl + "/comm/userInfoAndSchoolInfo/schoolId/" + schoolId + "/userId/" + currentUserId,
                Map.class);
        String userNick = map.get("userNick") == null ? "" : map.get("userNick");
        String avatar = map.get("avatar") == null ? "" : map.get("avatar");
        String schoolNick = map.get("schoolNick") == null ? "" : map.get("schoolNick");

        // 创建学校内部通知
        YxyNoticeInner yxyNoticeInner = new YxyNoticeInner();
        yxyNoticeInner.setUserId(currentUserId);
        yxyNoticeInner.setSchoolId(schoolId);
        yxyNoticeInner.setSchoolNick(schoolNick);
        yxyNoticeInner.setTitle(reqModel.title);
        yxyNoticeInner.setContent(reqModel.content);
        yxyNoticeInner.setReceiver(receiver);
        yxyNoticeInner.setAttachmentName(reqModel.attachmentName);
        yxyNoticeInner.setAttachmentPath(reqModel.attachmentPath);
        yxyNoticeInner.setDeleteStatus(0);
        yxyNoticeInner.setImage(reqModel.image);
        yxyNoticeInner.setUserNick(userNick);
        yxyNoticeInner.setAvatar(avatar);
        yxyNoticeInnerRepository.save(yxyNoticeInner);
        respModel.setMessage("发通知成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 标题
        public String title;
        // 内容
        public String content;
        // 接受对象列表
        public List<String> receiver;
        // 接受上传图片
        public List<String> image;
        // 附件原始文件名称
        public String attachmentName;
        // 附件地址
        public String attachmentPath;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(content), "内容不能为空");
            Assert.isTrue(content.length() > 1 && content.length() < 701, "内容要在2-700个文字之间");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
